﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WFExtended.Framework.ServiceModel
{
    /// <summary>
    /// Service Error Notifier
    /// </summary>
    public class ServiceErrorNotifier
    {
        private static Dictionary<Type, IServiceErrorObserver> _observers = new Dictionary<Type, IServiceErrorObserver>();

        internal static void NotifyErrror(Exception exception)
        {
            foreach (IServiceErrorObserver observer in _observers.Values)
                observer.OnError(exception);
        }

        /// <summary>
        /// Adds an error observer
        /// </summary>
        /// <param name="observer">The error observer.</param>
        public static void AddObserver(IServiceErrorObserver observer)
        {
            if (!_observers.ContainsKey(observer.GetType()))
                _observers.Add(observer.GetType(), observer);
        }

        /// <summary>
        /// Detaches an error observer
        /// </summary>
        /// <param name="observerType">Type of the observer.</param>
        public static void DetachObserver(Type observerType)
        {
            if (_observers.ContainsKey(observerType))
                _observers.Remove(observerType);
        }
    }
}
